package com.szsh.aiot.hsm.mapper;

import com.szsh.aiot.hsm.dao.ISubinfoSyncRet;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ISubinfoSyncRetMapper {

    @Insert({
        "insert into I_SUBINFO_SYNC_RET (broadbandNO,gatewayMac, syncTime, secResult,",
        "cameraResult,vprovsetResult,secConfigResult,secConfigResultDesc,cameraConfigResult, ",
        "cameraConfigResultDesc,vprovsetConfigResult,vprovsetConfigResultDesc, batchID) ",
        "values (#{broadbandNO,jdbcType=VARCHAR},#{gatewayMac,jdbcType=VARCHAR}, ",
        " #{syncTime,jdbcType=TIMESTAMP}, #{secResult,jdbcType=INTEGER}, ",
        "#{cameraResult,jdbcType=INTEGER},#{vprovsetResult,jdbcType=INTEGER}, ",
        "#{secConfigResult}, #{secConfigResultDesc},#{cameraConfigResult},",
        "#{cameraConfigResultDesc}, #{vprovsetConfigResult},#{vprovsetConfigResultDesc}, #{batchID})",
    })
    int insert(ISubinfoSyncRet record);

    @Select({
        "select * from I_SUBINFO_SYNC_RET where broadbandNO = #{broadbandNO} and gatewayMac = #{gatewayMac}"
    })
    ISubinfoSyncRet selectByBbn(@Param("broadbandNO") String broadbandNO,
            @Param("gatewayMac") String gatewayMac);

    @Delete("delete from I_SUBINFO_SYNC_RET where broadbandNO = #{broadbandNO}")
    int deleteByBbn(String broadbandNO);

    @Delete("delete from I_SUBINFO_SYNC_RET where broadbandNO = #{broadbandNO} and gatewayMac = #{gatewayMac}")
    int deleteByBroadbandNoAndGateWayMac(@Param("broadbandNO") String broadbandNO, @Param("gatewayMac") String gatewayMac);

    @Update({
        "update I_SUBINFO_SYNC_RET set secResult=#{secResult}, secConfigResult=#{secConfigResult}, ",
        "secConfigResultDesc=#{secConfigResultDesc}, syncTime=#{syncTime}, batchID=#{batchID}",
        "where broadbandNO = #{broadbandNO} and gatewayMac = #{gatewayMac}" })
    int updateSecResult(ISubinfoSyncRet syncRet);

    @Update({
        "update I_SUBINFO_SYNC_RET set cameraResult=#{cameraResult}, cameraConfigResult=#{cameraConfigResult}, ",
        "cameraConfigResultDesc=#{cameraConfigResultDesc}, syncTime=#{syncTime}, batchID=#{batchID}",
        "where broadbandNO = #{broadbandNO} and gatewayMac = #{gatewayMac}" })
    int updateCameraResult(ISubinfoSyncRet syncRet);

    @Update({
        "update I_SUBINFO_SYNC_RET set vprovsetResult=#{vprovsetResult}, vprovsetConfigResult=#{vprovsetConfigResult}, ",
        "vprovsetConfigResultDesc=#{vprovsetConfigResultDesc}, syncTime=#{syncTime}, batchID=#{batchID}",
        "where broadbandNO = #{broadbandNO} and gatewayMac = #{gatewayMac}" })
    int updateVprovsetResult(ISubinfoSyncRet syncRet);

    /**
     * 查询所有流量采集订购失败的网关账号和网关MAC 分页
     *
     * @return List<ResendOrder>
     */
    @Select({"<script>",
        "SELECT * FROM I_SUBINFO_SYNC_RET",
        "<where>",
            "<if test ='broadbandNO != null and broadbandNO !=\"\"'> ",
                "and broadbandNO like concat('%', concat(#{broadbandNO},'%'))",
            "</if>",
            "<if test ='beginDate != null and beginDate !=\"\"'>",
                "and syncTime >= #{beginDate}",
            "</if>",
            "<if test ='endDate != null and endDate !=\"\"'>",
                "and #{endDate} >= syncTime",
            "</if>",
            "<if test ='batchID != null and batchID !=\"\"'>",
                "and batchID = #{batchID}",
            "</if>",
            // 成功
            "<if test ='status != null and status !=\"\" and status == \"0\"'>",
                "and (secConfigResult = 0 and cameraConfigResult = 0 and vprovsetConfigResult = 0)",
            "</if>",
            // 失败
            "<if test ='status != null and status !=\"\" and status == \"-1\"'>",
                "and ((secConfigResult is null or secConfigResult = -1) ",
                "or (cameraConfigResult is null or cameraConfigResult = -1) ",
                "or (vprovsetConfigResult is null or vprovsetConfigResult = -1)) ",
            "</if>",
        "</where>",
        "order by syncTime desc ",
        "<if test ='limit > 0'> limit #{start}, #{limit}</if>",
    "</script>"})
    List<ISubinfoSyncRet> queryPagination(@Param("broadbandNO") String broadbandNO,
            @Param("beginDate") String beginDate, @Param("endDate") String endDate,
            @Param("status") String status, @Param("batchID") String batchID,
            @Param("start") int start, @Param("limit") int limit);

    @Select({"<script>",
        "SELECT count(*) FROM `I_SUBINFO_SYNC_RET`",
        "<where>",
            "<if test ='broadbandNO != null and broadbandNO !=\"\"'> ",
                "and broadbandNO like concat('%', concat(#{broadbandNO},'%'))",
            "</if>",
            "<if test ='beginDate != null and beginDate !=\"\"'>",
                "and syncTime >= #{beginDate}",
            "</if>",
            "<if test ='endDate != null and endDate !=\"\"'>",
                "and #{endDate} >= syncTime",
            "</if>",
            // 成功
            "<if test ='status != null and status !=\"\" and status == \"0\"'>",
                "and (secConfigResult = 0 and cameraConfigResult = 0 and vprovsetConfigResult = 0)",
            "</if>",
            // 失败
            "<if test ='status != null and status !=\"\" and status == \"-1\"'>",
                "and ((secConfigResult is null or secConfigResult = -1) ",
                "or (cameraConfigResult is null or cameraConfigResult = -1) ",
                "or (vprovsetConfigResult is null or vprovsetConfigResult = -1)) ",
            "</if>",
        "</where>",
    "</script>"})
    int count(@Param("broadbandNO") String broadbandNO, @Param("beginDate") String beginDate,
            @Param("endDate") String endDate, @Param("status") String status);
}
